﻿#include "OiPCH.hpp"
#include "Appl/OiOperStudio.hpp"
#include "View/OiRaster.hpp"

#include <QtCharts\QLineSeries>
#include <QtCharts\QPieSeries>
#include <QtCharts\QScatterSeries>
#include <QtCharts\QSplineSeries>
#include <QtCharts\QValueAxis>

namespace Oi
{
    namespace PLScannerCore
    {
        class CLTDataDlgImpl : public Impl<CLTDataDlg>
        {

        public:
            void init(QString file);
            void exit();

        public:
            CLTProfileView* _measure;
            Appl::Chart* _chart;
            Appl::OperStudio* _flawDisplay;
            Core::PointMapPtr _flaw;
            LTBase::MeasurementSnapShot snapShot;
        };

        void CLTDataDlgImpl::init(QString filePath)
        {
            OI_Q(CLTDataDlg);

            _measure = NULL;
            _chart = NULL;
            _flawDisplay = NULL;

            QHBoxLayout* layout = new QHBoxLayout(q); 
            //_widgets = new QStackedWidget(q);

            QFileInfo info(filePath);

            if(info.exists())
            {
                if(info.suffix().toLower() == QLatin1String("snp"))
                {
                    QFile file(filePath);
                    if(!file.open(QIODevice::ReadOnly))
                    {
                        return;
                    }

                    QDataStream in(&file);
                    in >> snapShot;

                    _measure = new CLTProfileView(q);
                    _chart = _measure->chart();

                    

                    layout->addWidget(_measure);
                    q->resize(QSize(1280, 720));
                }

                else if(info.suffix().toLower() == QLatin1String("pmf"))
                {
                    _flawDisplay = new Appl::OperStudio(q);
                    _flaw = Core::PointMap::create(filePath);

                    layout->addWidget(_flawDisplay);
                    q->resize(QSize(1280, 720));
                }
            }

            QMetaObject::invokeMethod(q, "onReady", Qt::QueuedConnection);
        }

        void CLTDataDlgImpl::exit()
        {

        }

        CLTDataDlg::CLTDataDlg(QString file, QWidget *parent)
            : Ctrl::Dialog(parent)
        {
            OI_I(CLTDataDlg)->init(file);

        }

        CLTDataDlg::~CLTDataDlg()
        {
            OI_E(CLTDataDlg)->exit();
        }

        void CLTDataDlg::onReady()
        {
            OI_F(CLTDataDlg);
            if (d->_flaw)
            {
                d->_flawDisplay->show3DLayer();
                d->_flawDisplay->raster()->display(View::Raster::Persp);
                d->_flawDisplay->raster()->display(d->_flaw, -1, -1, Core::PointMapPtr(),LT360X::instance()->connectionThreshold());

            }
            if (d->_measure)
            {
                d->_chart->measurementSnapShot(d->snapShot);
            }
        }

    }
}

